part of _;

extension on ChatView {
  void doMessageTap(MessageModel message) {
    switch (message.messageType) {
      case Enum_MessageType.Text:
        break;
      case Enum_MessageType.Voice:
        if (message.isVoicePlaying) {
          message.isVoicePlaying = false;
          page.setState(() {});
          page.controller_chat!.state.player.stop();
          return;
        }
        for (final message in page.controller_chat!.state.messages) {
          message.isVoicePlaying = false;
        }
        message.isVoicePlaying = true;
        message.isVoicePlayed = true;
        page.setState(() {});
        Map<String, dynamic> content = json.decode(message.content);
        int second = content["second"];
        String url = content["url"];
        page.controller_chat!.state.player.stop();
        page.controller_chat!.state.player.play(url.startsWith("http") ? UrlSource(url) : DeviceFileSource(url));
        page.controller_chat!.state.player.onPlayerComplete.listen((event) {
          message.isVoicePlaying = false;
          page.setState(() {});
        });
        YIM.messageManager.queryLocalMessagesByID(
          conversationType: YIMEnum_ConversationType.P2P,
          conversationID: page.controller_conversation!.getSelectConversation()!.conversationID,
          messageID: message.id,
          callback: (message) {
            YIM.messageManager.updateMessage(
              message: message!..localExt = {"isVoicePlayed": true},
            );
          },
        );
        break;
      case Enum_MessageType.Image:
        openWindow(WindowType.ShowImage, params: {
          'images': [message.content],
        });
        break;
      case Enum_MessageType.Video:
        page.yIntent(ShowVideoPage(video: message.content), intentType: YIntentType.Present);
        break;
      default:
        break;
    }
  }

  void doMessageSecondaryTap(TapDownDetails details, MessageModel message) {
    PopupMenuItem view_item({required IconData icon, required String title, bool isRed = false, required Function() onTap}) {
      return PopupMenuItem(
        child: Row(
          children: [
            Icon(icon, color: isRed ? Colors.red : Colors.grey.shade600, size: 20),
            YSpacer(width: 10),
            Text(title, style: TextStyle(color: isRed ? Colors.red : Colors.grey.shade600, fontSize: 14)),
          ],
        ),
        onTap: () => onTap(),
      );
    }

    final isSendSuccess = message.messageState == YIMEnum_MessageState.Success_Read || message.messageState == YIMEnum_MessageState.Success_UnRead;
    List<PopupMenuEntry> items = [];
    if (message.isMine) {
      if (isSendSuccess) {
        if (DateTime.now().millisecondsSinceEpoch - message.date < 2 * 60 * 1000) {
          items.add(
            view_item(
              icon: Icons.undo_rounded,
              title: "撤回",
              onTap: () {
                YIM.messageManager.sendMessageRevoke(
                  conversationType: YIMEnum_ConversationType.P2P,
                  conversationID: page.controller_conversation!.getSelectConversation()!.conversationID,
                  messageID: message.id,
                  callback: (isSuccess) {
                    if (!isSuccess) {
                      yToast("撤回失败");
                      return;
                    }
                    page.setState(() => message.isRevoke = true);
                  },
                );
              },
            ),
          );
        }
      }
    }
    if (isSendSuccess) {
      items.add(
        view_item(
          icon: Icons.delete_rounded,
          title: "删除",
          isRed: true,
          onTap: () {
            YIM.messageManager.deleteLocalMessage(
              conversationType: YIMEnum_ConversationType.P2P,
              conversationID: page.controller_conversation!.getSelectConversation()!.conversationID,
              messageID: message.id,
            );
            page.controller_chat!.state.messages.removeWhere((element) => element.id == message.id);
            page.setState(() {});
            yToast("删除成功");
          },
        ),
      );
    }
    switch (message.messageType) {
      case Enum_MessageType.Text:
        items.insert(
          0,
          view_item(
            icon: Icons.copy_rounded,
            title: "复制",
            onTap: () {
              yCopyClipBoard(message.content);
              yToast("复制成功");
            },
          ),
        );
        break;
      case Enum_MessageType.Voice:
        break;
      case Enum_MessageType.Image:
        break;
      case Enum_MessageType.Video:
        break;
      default:
        break;
    }
    if (items.isNotEmpty) {
      showMenu(
        context: page.context,
        position: RelativeRect.fromRect(Rect.fromLTRB(details.globalPosition.dx, details.globalPosition.dy, details.globalPosition.dx + 110, details.globalPosition.dy - 40), Offset.zero & (Overlay.of(page.context).context.findRenderObject() as RenderBox).size),
        items: items,
      );
    }
  }
}
